package com.antdesignbackend.backend.service.impl;

import com.antdesignbackend.backend.common.CalendarDate;
import com.antdesignbackend.backend.entity.Appointment;
import com.antdesignbackend.backend.entity.Room;
import com.antdesignbackend.backend.entity.entityVo.AppointmentVo;
import com.antdesignbackend.backend.mapper.AppointmentMapper;
import com.antdesignbackend.backend.service.IAppointmentService;
import com.antdesignbackend.backend.service.IRoomService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class AppointmentServiceImpl extends ServiceImpl<AppointmentMapper, Appointment> implements IAppointmentService {
    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private IRoomService roomService;
    @Autowired
    private IAppointmentService appointmentService;


    @Override
    public List<AppointmentVo> search(QueryWrapper<AppointmentVo> queryWrapper, Page<AppointmentVo> page) {
        return appointmentMapper.search(queryWrapper, page);
    }
    @Override
    public List<AppointmentVo> searchs(QueryWrapper<AppointmentVo> queryWrapper) {
        return appointmentMapper.searchs(queryWrapper);
    }
    @Override
    public Integer count(QueryWrapper<AppointmentVo> queryWrapper) {
        return appointmentMapper.count(queryWrapper);
    }
    @Override
    public AppointmentVo total(QueryWrapper<AppointmentVo> queryWrapper) {
        return appointmentMapper.total(queryWrapper);
    }

    @Override
    public Boolean IsAppointment(Integer roomId, String startTime, String endTime) {
        Boolean b=false;
        try
        {
            log.error("预约查看：1111111111111111111");
            QueryWrapper<Appointment> queryWrapper = new QueryWrapper<>();
            queryWrapper.ge("state",0);  //>=0
            queryWrapper.and(wra -> wra.gt("startTime", startTime).lt("startTime", endTime).or().gt("endTime", startTime).lt("endTime", endTime));

            List<Appointment> datalist=appointmentMapper.selectList(queryWrapper);
            if(datalist.size()==0)
            {
                b=true;
            }

        }
        catch(Exception ex)
        {
             log.error("预约查看异常："+ex.getMessage());
        }


        return b;
    }
    @Override
    public Boolean IsAppoint(Integer roomId, String startTime, String endTime) {
        Boolean b=false;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //起始时间
        Calendar calendars = Calendar.getInstance();
        try {
            calendars.setTime(df.parse(startTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        calendars.add(Calendar.MINUTE, +30);
        String addStartTime=df.format(calendars.getTime());

        Calendar calendars1 = Calendar.getInstance();
        try {
            calendars1.setTime(df.parse(startTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        calendars1.add(Calendar.MINUTE, -30);
        String cutStartTime=df.format(calendars1.getTime());
        //结束时间
        Calendar calendare = Calendar.getInstance();
        try {
            calendare.setTime(df.parse(endTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        calendare.add(Calendar.MINUTE, +30);
        String addEndTime=df.format(calendare.getTime());
        Calendar calendare1 = Calendar.getInstance();
        try {
            calendare1.setTime(df.parse(endTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        calendare1.add(Calendar.MINUTE, -30);
        String cutEndTime=df.format(calendare1.getTime());
        try
        {
            QueryWrapper<Appointment> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("state",1);
            queryWrapper.eq("roomId",roomId);
            queryWrapper.and(wra -> wra.le("startTime", addStartTime).ge("endTime", cutStartTime).or().ge("endTime", cutEndTime).le("startTime", addEndTime).or().ge("startTime", startTime).le("endTime", endTime));
            List<Appointment> datalist=appointmentMapper.selectList(queryWrapper);
            if(datalist.size()==0)
            {
                b=true;
            }
        }
        catch(Exception ex)
        {
            log.error("预约查看异常："+ex.getMessage());
        }
        return b;
    }

    @Override
    public Boolean AddAppointment(AppointmentVo vo) {
        Boolean b=false;
        long mins=0;
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateFormat dfdate = new SimpleDateFormat("yyyy-MM-dd");
        DateFormat dfHHmm = new SimpleDateFormat("HH:mm");
        try
        {
            Date startTime=df.parse(vo.getStartTimeStr());
            Date endTime=df.parse(vo.getEndTimeStr());
            String startHHmm=vo.getStartTimeStr().substring(11,16).replace(":","");
            String endHHmm=vo.getEndTimeStr().substring(11,16).replace(":","");
            Appointment data=new Appointment();
            Room roomdata=roomService.getById(vo.getRoomId());
            if(roomdata!=null && roomdata.getState().equals(1))
            {
                mins= CalendarDate.dateDiff(vo.getStartTimeStr(),vo.getEndTimeStr(),"min");
                data.setStartTime(startTime);
                data.setEndTime(endTime);
                data.setCreateTime(new Date());
                data.setState(vo.getState());
                data.setPrice(roomdata.getPrice());
                data.setAmount((roomdata.getPrice()/60)*mins);
                data.setWeChat(vo.getWeChat());
                data.setRoomId(vo.getRoomId());
                data.setAppointDate(dfdate.parse(vo.getStartTimeStr()));
            }
        }
        catch(Exception ex)
        {
            log.error("预约增加异常："+ex.getMessage());
        }
        return b;
    }
    @Override
    public Boolean peoAddAppointment(AppointmentVo vo) {
        Boolean b=false;
        long mins=0;
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateFormat dfdate = new SimpleDateFormat("yyyy-MM-dd");
        try
        {
            String action=vo.getAction();
            Date startTime=df.parse(vo.getStartTimeStr());
            Date endTime=df.parse(vo.getEndTimeStr());
            String startHHmm=vo.getStartTimeStr().substring(11,16).replace(":","");
            String endHHmm=vo.getEndTimeStr().substring(11,16).replace(":","");
            Appointment data=new Appointment();
            Room roomdata=roomService.getById(vo.getId());
            if(roomdata!=null && roomdata.getState().equals(1)){
                mins= CalendarDate.dateDiff(vo.getStartTimeStr(),vo.getEndTimeStr(),"min");
                data.setStartTime(startTime);
                data.setEndTime(endTime);
                data.setCreateTime(new Date());
                data.setPrice(roomdata.getPrice());
                if("artificial".equals(action)){
                    data.setState(0);
                    data.setAmount((roomdata.getPrice()/60)*mins);
                    data.setTypes(3);
                }
                if("account".equals(action)){
                    data.setState(1);
                    data.setAmount(0.0);
                    data.setTypes(4);
                }
                data.setRoomId(vo.getId());
                data.setShopId(roomdata.getShopId());
                data.setAppointDate(dfdate.parse(vo.getStartTimeStr()));
                data.setStartInt(Integer.valueOf(startHHmm));
                data.setEndInt(Integer.valueOf(endHHmm));
                data.setIsMessage(0);
                data.setEvaluateScore(0);
                data.setClientName(vo.getClientName());
                data.setClientPhone(vo.getClientPhone());
                data.setIsMessageAdmin(0);
                data.setStreamFlag(0);
                data.setWeChat("");
                data.setOrderNumber(paymentID());
                b=appointmentService.save(data);
            }
        }
        catch(Exception ex)
        {
            log.error("预约增加异常："+ex.getMessage());
        }
        return b;
    }
    //产生订单号
    public String paymentID(){
        int r1=(int)(Math.random()*(10));//产生2个0-9的随机数
        int r2=(int)(Math.random()*(10));
        long now = System.currentTimeMillis();//一个13位的时间戳
        String paymentID0 =String.valueOf(r1)+String.valueOf(r2)+String.valueOf(now);
        int r3=(int)(Math.random()*(10));//产生2个0-9的随机数
        int r4=(int)(Math.random()*(10));
        String paymentID = paymentID0 +String.valueOf(r3)+String.valueOf(r4);// 订单ID
        return paymentID;
    }

}
